import 'dart:async';
import 'dart:io';

import 'package:cloud_phone_manager/utils/FileUtils.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_styled_toast/flutter_styled_toast.dart';

import '../Callback/Callback.dart';
import '../data/TabData.dart';
import '../dialog/AppManagerDialog.dart';
import '../dialog/ScrcpyConnectDialog.dart';
import '../utils/CmdUtils.dart';

/// 导航功能条
class HeaderTabView extends StatelessWidget{
  List<TabData> _functionList = [];
  HeaderTabClickCallback headerTabClickCallback;
  StreamSubscription<String>? scrcpyCaallback;
  Process? scrcpyProcess;

  HeaderTabView({super.key,required this.headerTabClickCallback});

  void showAppManagerDialog(BuildContext context) {
    showDialog(context: context, builder: (BuildContext context){
      return AppManagerDialog(title: "ADB", onClosed: ()=>{
        Navigator.pop(context)
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    if(_functionList.isEmpty){
      //设备信息
      // _functionList.add(TabData("设备信息", 6));
      //Log日志
      _functionList.add(TabData("日志", 0));
      //投屏
      _functionList.add(TabData("投屏", 1));
      //安装应用
      _functionList.add(TabData("安装应用", 2));
      //文件管理
      _functionList.add(TabData("文件管理", 3));
      //应用管理
      _functionList.add(TabData("应用管理", 4));
      //进程管理
      _functionList.add(TabData("进程管理", 5));
      _functionList.add(TabData("AM", 13));
      _functionList.add(TabData("SM", 14));
      //重启
      // _functionList.add(TabData("重启", 6));
      //关机
      // _functionList.add(TabData("关机", 7));
      //recovery模式
      // _functionList.add(TabData("RE模式", 8));
      //进入Fastboot模式
      // _functionList.add(TabData("Fastboot", 9));
      //重启adb服务
      // _functionList.add(TabData("重启ADB", 10));
      //瑞驰改机
      // _functionList.add(TabData("瑞驰改机", 11));
      //福州改机
      // _functionList.add(TabData("福州改机", 12));
    }
    return Container(
      padding: const EdgeInsets.fromLTRB(10, 5, 10, 5),
      child: ListView.builder(
        scrollDirection: Axis.horizontal,
        itemCount: _functionList.length, // 宫格数量
        itemBuilder: (BuildContext context, int index) {
          return Container(margin: EdgeInsets.fromLTRB(0, 0, 10, 0),child: ClipRRect(
              borderRadius: BorderRadius.circular(10),
              child: Center(
                child: Container(
                  color: Colors.amber,
                  alignment: Alignment.center,
                  child: TextButton(
                    onPressed: () {
                      headerTabClickCallback.call(_functionList[index]);
                      if(_functionList[index].index == 1){
                        //投屏
                        showDialog(context: context, builder: (BuildContext context){
                          return ScrcpyConnectDialog(title: "", onClosed: ()=>{
                            Navigator.pop(context)
                          },onConfirm: (malv,size,fps)=>{
                            if(scrcpyCaallback != null){
                              scrcpyCaallback?.cancel()
                            },
                            scrcpyProcess?.kill(),
                            CmdUtils.connectToScrcpy(malv,size,fps, (success, error,process,suListener) {
                              scrcpyCaallback = suListener;
                              scrcpyProcess = process;
                            }),
                            Navigator.pop(context)
                          },);
                        });
                      }else if(_functionList[index].index ==2){
                        FileUtils.pickFile().then((value) => {
                          debugPrint("地址为：${value?.paths?.first??""}"),
                          CmdUtils.installApp(value?.paths?.first??"", (success, error, process, successListener) {
                            if(error.isEmpty || error == "DONE"){
                              showToast("安装成功",context: context);
                            }else{
                              debugPrint("安装失败：$error");
                              showToast("安装失败：${error}",context: context);
                            }
                          })
                        });
                      }else if(_functionList[index].index == 6){
                        CmdUtils.restart();
                      }else if(_functionList[index].index == 7){
                        CmdUtils.powerOff();
                      }else if(_functionList[index].index == 8){
                        CmdUtils.recovery();
                      }else if(_functionList[index].index == 9){
                        CmdUtils.fastboot();
                      }else if(_functionList[index].index == 10){
                        CmdUtils.rebootAdb();
                      }
                    },
                    child: Text(_functionList[index].name,
                        style: const TextStyle(fontSize: 12),
                        textAlign: TextAlign.center),
                  ),
                ),
              )),);
        },
      ),
    );
  }

}